package com.xjl.meituwidget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private TextView etPath;
    public static final String SP_NAME = "sp_table";

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etPath = findViewById(R.id.et_path);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        final String path = SpHelper.getInstance().getString("path");
        Log.i(TAG, "path:" + path);
        final File filePath = new File(path);
        Log.i(TAG, "filePath exists:" + filePath.exists());
        final File[] files = filePath.listFiles();
        if (files != null) {
            Log.i(TAG, "img_path:" + files[0].getPath());
            final Bitmap bitmap = BitmapFactory.decodeFile(files[0].getPath());
            ((ImageView) findViewById(R.id.iv)).setImageBitmap(bitmap);
        }
    }

    @Override
    public void onClick (View v) {
        SpHelper.getInstance().edit()
                .putString("path", Environment.getExternalStorageDirectory().getPath()+etPath.getText().toString().trim())
                .commit();
        Toast.makeText(this, "设置成功", Toast.LENGTH_LONG).show();
    }
}